package com.webmarmun.newsreader;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.TextView;

public class NewsreaderActivity extends Activity implements OnClickListener {
	
	LinearLayout linearLayout;
	ImageView img;
	int totalviews;
    
	Button slideHandleButton;
	SlidingDrawer slidingDrawer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.panelview);
        PanelPager realViewSwitcher = (PanelPager)findViewById(R.id.realPager);
		linearLayout = (LinearLayout)findViewById(R.id.dotsLinear);
		
		realViewSwitcher.setOnScreenSwitchListener(onScreenSwitchListener);
		int j=0;
		boolean loadView = false;
		View v = null; 
		boolean firstLoad = true;
		
		List dataList = new ArrayList();
		addDatatoList(dataList);
		
		
		for(int i=0; i<dataList.size(); i++) {
			if(loadView){
				loadView = false;
				v = View.inflate(this, R.layout.panellinearview, null);
			}else{
				if(i == 0){
					v = View.inflate(this, R.layout.panellinearview, null);
				}
				if( j > 4){
					loadView = true;
				}
			}
		
			DataDTO mdto = (DataDTO) dataList.get(i);
			if( j == 0){
				LinearLayout f = (LinearLayout) v.findViewById(R.id.frameView1);
				f.setVisibility(View.VISIBLE);
				TextView t = (TextView) v.findViewById(R.id.textView1);	
				t.setText(mdto.getName());
				img = (ImageView) v.findViewById(R.id.imageView1);
				img.setOnClickListener(this);
				String imageUrl = mdto.getImageURL();
				 if (imageUrl != null)
		 	    	{
					    LazyLoadImage downloader = new LazyLoadImage(img);
			 			downloader.execute(imageUrl);	
		 			}
			}else if( j == 1){
				LinearLayout f = (LinearLayout) v.findViewById(R.id.frameView2);
				f.setVisibility(View.VISIBLE);
				TextView t = (TextView) v.findViewById(R.id.textView2);
				t.setText(mdto.getName());
				img = (ImageView) v.findViewById(R.id.imageView2);
				
				img.setOnClickListener(this);
				String imageUrl = mdto.getImageURL();
				 if (imageUrl != null)
		 	    	{
						 LazyLoadImage downloader = new LazyLoadImage(img);
				 		 downloader.execute(imageUrl);
		 	    }
			}else if( j == 2){
				LinearLayout f = (LinearLayout) v.findViewById(R.id.frameView3);
				f.setVisibility(View.VISIBLE);
				TextView t = (TextView) v.findViewById(R.id.textView3);
				t.setText(mdto.getName());
				img = (ImageView) v.findViewById(R.id.imageView3);
				
				img.setOnClickListener(this);
				String imageUrl = mdto.getImageURL();
				 if (imageUrl != null)
		 	    	{
					 LazyLoadImage downloader = new LazyLoadImage(img);
			 		 downloader.execute(imageUrl);
		 	        }
			}else if( j == 3){
				LinearLayout f = (LinearLayout) v.findViewById(R.id.frameView4);
				f.setVisibility(View.VISIBLE);
				TextView t = (TextView) v.findViewById(R.id.textView4);
				t.setText(mdto.getName());
				img = (ImageView) v.findViewById(R.id.imageView4);
				
				img.setOnClickListener(this);
				String imageUrl = mdto.getImageURL();
				 if (imageUrl != null)
		 	    	{
						 LazyLoadImage downloader = new LazyLoadImage(img);
				 		 downloader.execute(imageUrl);
		 	    	}
			}else if( j == 4){
				LinearLayout f = (LinearLayout) v.findViewById(R.id.frameView5);
				f.setVisibility(View.VISIBLE);
				TextView t = (TextView) v.findViewById(R.id.textView5);
				t.setText(mdto.getName());
				img = (ImageView) v.findViewById(R.id.imageView5);
				
				img.setOnClickListener(this);
				String imageUrl = mdto.getImageURL();
				 if (imageUrl != null)
		 	    	{
						 LazyLoadImage downloader = new LazyLoadImage(img);
				 		 downloader.execute(imageUrl);
		 	    	}
			}else if( j == 5){
				LinearLayout f = (LinearLayout) v.findViewById(R.id.frameView6);
				f.setVisibility(View.VISIBLE);
				TextView t = (TextView) v.findViewById(R.id.textView6);
				t.setText(mdto.getName());
				img = (ImageView) v.findViewById(R.id.imageView6);
				
				img.setOnClickListener(this);
				String imageUrl = mdto.getImageURL();
				 if (imageUrl != null)
		 	    	{
						 LazyLoadImage downloader = new LazyLoadImage(img);
				 		 downloader.execute(imageUrl);
		 	    	}
			}
			if(j == 5 || (i == dataList.size() - 1)){
				j = 0;
				realViewSwitcher.addView(v);
				totalviews = totalviews + 1;
			}else{
				j++;	
			}
		}
		setImages(0);

		
      
       
        
       /*
        * gallery
        */
        DisplayMetrics metrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
      
             /*
              * drawer view
              */
             
             slideHandleButton = (Button) findViewById(R.id.slideHandleButton);
             slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);

             slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {

             @Override
             public void onDrawerOpened() {
            // slideHandleButton.setBackgroundResource(R.drawable.ic_launcher);
             	//scroll.setVisibility(ScrollView.GONE);
             }
             });

             slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

             @Override
             public void onDrawerClosed() {
             	//scroll.setVisibility(ScrollView.VISIBLE);
             //slideHandleButton.setBackgroundResource(R.drawable.action_bar_logo);
             }
             });

      
        
        
        



    }
    

    
   

private final PanelPager.OnScreenSwitchListener onScreenSwitchListener = new PanelPager.OnScreenSwitchListener() {
		
		@Override
		public void onScreenSwitched(int screen) {
			// this method is executed if a screen has been activated, i.e. the screen is completely visible
			//  and the animation has stopped (might be useful for removing / adding new views)
			setImages(screen);
		}
		
	};
	
	private void setImages(int screen){
		try {
			if(totalviews > 1){
				linearLayout.removeAllViews();
				for(int i=0; i < totalviews; i++){
					if(i == screen){
						ImageView imageView = new ImageView(this);
						Drawable drawable1 = this.getResources().getDrawable(R.drawable.whitedot);
						imageView.setImageDrawable(drawable1);
						imageView.setPadding(0, 0, 10, 0);
						linearLayout.addView(imageView);
					}else{
						ImageView imageView = new ImageView(this);
						Drawable drawable1 = this.getResources().getDrawable(R.drawable.greydot);
						imageView.setImageDrawable(drawable1);
						imageView.setPadding(0, 0, 10, 0);
						linearLayout.addView(imageView);
					}
				}
			}			
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
	}
	
	public void onClick(View v) {
		try {
			if(v instanceof ImageView){
				ImageView imageView = (ImageView)v;
				String selectedItem = (String)imageView.getTag();
			}			
		} catch (Exception e) {
			
		}		
	}
	
	@SuppressWarnings("unchecked")
	private void addDatatoList(@SuppressWarnings("rawtypes") List dataList){
		DataDTO dataDTO1 = new DataDTO();
		dataDTO1.setName("First Tab");
		dataDTO1.setImageURL("http://prvisud.rs/slike/seminarparnica_thumb.jpg");
		dataList.add(dataDTO1);
		
		DataDTO dataDTO2 = new DataDTO();
		dataDTO2.setName("Second Tab");
		dataDTO2.setImageURL("http://prvisud.rs/slike/okr_prez_thumb.jpg");
		dataList.add(dataDTO2);

		DataDTO dataDTO3 = new DataDTO();
		dataDTO3.setName("Third Tab");
		dataDTO3.setImageURL("http://prvisud.rs/slike/invalidi_thumb.jpg");
		dataList.add(dataDTO3);
		
		DataDTO dataDTO4 = new DataDTO();
		dataDTO4.setName("Fourth Tab");
		dataDTO4.setImageURL("http://prvisud.rs/slike/novesudije1_thumb.jpg");
		dataList.add(dataDTO4);
		
		DataDTO dataDTO5 = new DataDTO();
		dataDTO5.setName("Fifth Tab");
		dataDTO5.setImageURL("http://prvisud.rs/slike/folders.jpg");
		dataList.add(dataDTO5);
		
		DataDTO dataDTO6 = new DataDTO();
		dataDTO6.setName("Sixth Tab");
		dataDTO6.setImageURL("http://prvisud.rs/slike/fruskagora.jpg");
		dataList.add(dataDTO6);
		
		DataDTO dataDTO7 = new DataDTO();
		dataDTO7.setName("Seventh Tab");
		dataDTO7.setImageURL("http://prvisud.rs/slike/pravni.jpg");
		dataList.add(dataDTO7);
		
		DataDTO dataDTO8 = new DataDTO();
		dataDTO8.setName("Eigth Tab");
		dataDTO8.setImageURL("http://prvisud.rs/slike/LogoSud.jpg");
		dataList.add(dataDTO8);
	}
}